#include "mouse.h"
#include "pictor.h"
#include <dos.h>
#include <stdio.h>

int _PL_mousepresent = FALSE;

/* Mouse code developed from the following template:
 * http://www.writeka.com/emage/mouse_events.html */

int initmouse()
{
    union REGS inreg, outreg;
    inreg.w.ax =0x0;
    int86(0x33,&inreg,&outreg);
    if(outreg.x.ax == -1)
    {
		_PL_mousepresent = TRUE;
    }
    return(outreg.x.ax);
}
/*
Input:
AX=0x0
Output:
AX=0 & BX=-1 means that no mouse is available.
AX=-1 & BX=number of mouse buttons if mouse is available.
*/ 


void showmouse()
{
    union REGS inreg, outreg;
    inreg.w.ax =0x1;
    int86(0x33,&inreg,&outreg);
}
/*
Input:
AX=0x1
Output:
Does Nothing is  pointer is already visible otherwise it makes pointer visible
*/ 


void hidemouse()
{
    union REGS inreg, outreg;
    inreg.w.ax=0x2;
    int86(0x33,&inreg,&outreg);
}
/*
Input:
AX=0x2
Output:
Does Nothing if already hidden, otherwise hides the mouse pointer
*/


void mousestatus( int *x, int *y, int *b )
{
    union REGS inreg, outreg;
    inreg.w.ax=0x3;
    inreg.w.bx=inreg.w.cx=inreg.w.dx=0;
    int86(0x33,&inreg,&outreg);
    *b=outreg.w.bx;
    *x=outreg.w.cx;
    *y=outreg.w.dx;
}
/*
Input:
AX=0x3
Output:
BX=number of the button pressed. 1-left 2-right 3-middle
CX=x co-ordinate
DX=y co-ordinate
*/

/* The above functions should be sufficient for most purposes. 
 * However to track button presses you can use set register AX as 0x5, 
 * register BX as the button number. Register BX will hold the number of 
 * presses after the interrupt call. Register CX and DX will hold values 
 * of the horizontal and vertical locations of the mouse pointer during 
 * the last button press. Similarly for button releases you can set the 
 * register AX as 0x6. The mouse pointer can be moved to desired location 
 * by setting register AX as 0x4 and registers CX and DX as the horizontal 
 * and vertical positions respectively. */

/* Returns 1 if a key is waiting in the keyboard buffer, 0 otherwise. */
int __cdecl querykeyboard()
{
	/* Call interrupt 16 and query the keyboard status (subfunction 0x01). */
	int key_waiting;
	__asm{
			mov ah, 0x01
			int 0x16
			jz no_keys
			mov key_waiting, 0x01
			jmp end
		no_keys:
			mov key_waiting, 0x00
		end:
		}
	return key_waiting;
}
	
 /* Becomes else-if statement. May need to be fixed. - W. Jones */
 
/* void __interrupt __far pictor_mouse_timer_int_1ch()
{
	static int x_coordinate, y_coordinate, button_press;
	mousestatus(&x_coordinate, &y_coordinate, &button_press);
	_chain_intr(old_timer_int_1ch);	
} */
